#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
int main(int argc, char const *argv[])
{
    int pipebuf[2];
    int piperesult = pipe(pipebuf);
    if (piperesult == -1)
    {
        perror("pipe");
        exit(EXIT_FAILURE);
    }
    pid_t pid = fork();
printf("父进程开始了\n");
    
    if (pid >0)
    {
        int result;
        printf("父进程准备向管道写入数据\n");
        close(pipebuf[0]);
        char *name = "G.E.M.邓紫棋";
        write(pipebuf[1], name, strlen(name));
        waitpid(pid,&result,0);
    }else if (pid == 0){
        printf("子进程准备从管道读取数据\n");
        close(pipebuf[1]);
        char name[20];
        read(pipebuf[0],name,sizeof(name));
        printf("读取到的数据是：%s\n",name);
    }
    printf("父进程结束了\n");
    
    return 0;
}
